package com.purchase.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.purchase.entity.Apply;
import com.purchase.entity.WxUser;
import com.purchase.entity.WxUserMember;
import com.purchase.mapper.ApplyMapper;
import com.purchase.mapper.WxUserMapper;
import com.purchase.service.ApplyService;
import com.purchase.util.DateUtil;
import com.purchase.util.LogInfo;
import com.purchase.util.RequestForm;
import com.purchase.util.ResponseForm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ApplyServiceImpl implements ApplyService {

    @Autowired
    private ApplyMapper applyMapper;

    @Autowired
    private WxUserMapper wxUserMapper;

    @Override
    public ResponseForm getList(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            com.purchase.util.Page.parsePage(paramMap);
            Page<Object> pageHelper = PageHelper.startPage((int) paramMap.get("page"), (int) paramMap.get("size"));
            List<Apply> list = applyMapper.selectList(paramMap);
            result.setData(list);
            result.setTotal((int) pageHelper.getTotal());
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("查询申请列表error", e);
        }
        return result;
    }

    @Override
    public ResponseForm add(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            Apply byId = applyMapper.selectByCardId(paramMap);
            if (byId != null) {
                if (byId.getStatus() == 0) {
                    result.setStatus(false);
                    result.setMessage("该用户状态：申请中");
                    return result;
                }
                if (byId.getStatus() == 1) {
                    result.setStatus(false);
                    result.setMessage("该用户状态：申请中");
                    return result;
                }
                if (byId.getStatus() == 2) {
                    result.setStatus(false);
                    result.setMessage("该用户状态：已审批通过");
                    return result;
                }
            }
            applyMapper.insertOne(paramMap);
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("创建申请error", e);
        }
        return result;
    }

    @Override
    public ResponseForm edit(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            //修改申请函（审核通过或拒绝）
            applyMapper.updateById(paramMap);
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("修改申请error", e);
        }
        return result;
    }

    @Override
    public ResponseForm getById(RequestForm param) {
        ResponseForm result = new ResponseForm();
        Map<String, Object> paramMap = (Map<String, Object>) param.getData();
        try {
            Apply byId = applyMapper.selectById(Integer.valueOf(String.valueOf(paramMap.get("id"))));
            result.setData(byId);
        } catch (Exception e) {
            result.setStatus(false);
            result.setMessage(LogInfo.ERROR);
            log.error("查询申请error", e);
        }
        return result;
    }
}
